home *** CD-ROM | disk | FTP | other *** search
Wrap
var enWebPersistenceThing; var enSaveFinishedInterval; var enSaveMainFile; var enDocumentToClip; var window_clipped_to_en; //dump("Loading mac clipper...\n"); // run through all children looking for image nodes. make sure the newImage.src // is equal to the originalImage.src. This would have been easier with XPaths or // NodeIterators but Firefox no support XPath queries on document fragments, // and NodeIterators are not available until Firefox 3.5 // The assumption here is that the tree structure of the nodes match. function evernote_processImagesInNodes(originalNode, newNode) { if (originalNode.nodeType == Node.ELEMENT_NODE && (originalNode.tagName == "img" || originalNode.tagName == "IMG")) { // found an image, make sure the new one is an image too if (newNode.nodeType == Node.ELEMENT_NODE && (newNode.tagName == "img" || newNode.tagName == "IMG")) { //dump("Found an image node: " + originalNode +"\n"); newNode.src = originalNode.src; return; } else { //dump("Found an image node but the new node doesn't match"); return; } } // these are not image nodes, recurse through the children var origChild = originalNode.firstChild; var newChild = newNode.firstChild; while (origChild) { evernote_processImagesInNodes(origChild, newChild); origChild = origChild.nextSibling; newChild = newChild.nextSibling; } } // fallback to the javascript clipper function evernote_javascriptClipperFallback () { window_clipped_to_en = document.commandDispatcher.focusedWindow; EN_CLIP_HOST='http://www.evernote.com'; EN_clip(EN_CLIP_HOST); } function evernote_performClip(domElement) { // check the OS version. If we're on Leopard or later, we're ok. // otherwise, fall-back to javascript clipper. // at some point this should be written a little more intelligently. if ((navigator.oscpu != "Intel Mac OS X 10.5") && (navigator.oscpu != "Intel Mac OS X 10.6")) { evernote_javascriptClipperFallback(); return true; } try { // make sure Evernote is installed var enClipper = Components.classes["@evernote.com/ENMacMozillaClipper;1"].createInstance(Components.interfaces.EvernoteMacMozillaClipper); var evernoteInstalled = 0; if (!enClipper.evernoteInstalled(evernoteInstalled)) { evernote_javascriptClipperFallback(); return true; } // assume the whole document enDocumentToClip = content.document; // FIXME:geech:2009-02-13 - break this logic into separate methods if (domElement && (domElement.tagName == "img" || domElement.tagName == "IMG")) { // create a new document where we'll add just the image. enDocumentToClip = content.document.implementation.createDocument (null, 'html', null); enDocumentToClip.URL = content.document.URL; var body = enDocumentToClip.createElementNS(null, 'body'); enDocumentToClip.documentElement.appendChild(body); // import the image node to ur new document var newImageNode = enDocumentToClip.importNode(domElement, true); // set the 'src' property of the image node to match the original. this will // make the absolute URL of the image the same as the original. otherwise // nsIWebBrowserPersist isn't always happy with relative image links. // note that this is *not* the same as the src attribute. newImageNode.src = domElement.src; body.appendChild(newImageNode); } else { // look for non-collapsed selections var sel = content.document.defaultView.getSelection(); var selCount = sel.rangeCount; if (selCount > 0) { var selectedContent = 0; for (var i = 0; i < selCount; i++) { var range = sel.getRangeAt(i); if (!range.collapsed) { selectedContent = 1; } } if (selectedContent) { // we have at least one non-collapsed range selected, so clip the selected ranges instead of the whole doc // first, create a document to hold the fragments enDocumentToClip = content.document.implementation.createDocument (null, 'html', null); enDocumentToClip.URL = content.document.URL; var body = enDocumentToClip.createElementNS(null, 'body'); enDocumentToClip.documentElement.appendChild(body); // add the fragments for (var i = 0; i < selCount; i++) { var range = sel.getRangeAt(i); var origFragment = range.cloneContents(); var newFragment = enDocumentToClip.importNode(origFragment, true); evernote_processImagesInNodes(origFragment, newFragment); body.appendChild(newFragment); } } } } // grab a reference to the temp dir var tempDir = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("TmpD", Components.interfaces.nsIFile); // create a temp dir in <tmp>/evernote-mac-clips/ tempDir.append("evernote-mac-clips"); tempDir.append("clip-tmp"); tempDir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0666); // our main file goes here enSaveMainFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); enSaveMainFile.initWithPath(tempDir.path); enSaveMainFile.append("firefox-clip.html"); // resources get stored here var resourcesDir = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); resourcesDir.initWithPath(tempDir.path); resourcesDir.append("resources"); // start saving the document //dump("Saving to "+enSaveMainFile.path+"\n"); enWebPersistenceThing = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist); enWebPersistenceThing.saveDocument(enDocumentToClip, enSaveMainFile, resourcesDir, null, 0, 0); // make sure we remove any currently running interval so we don't go into infinite clip land if the previous // clip hasn't finished if (enSaveFinishedInterval != null) { clearInterval(enSaveFinishedInterval); enSaveFinishedInterval = null; } // check every 250ms if the save has completed enSaveFinishedInterval = setInterval("evernote_checkDocumentSaveStatus()", 250); return true; } catch (e) { dump("Exception trying to clip:" + e); } // there was an exception of some sort during clipping, so fall back to the bookmarklet evernote_javascriptClipperFallback(); return true; } function evernote_checkDocumentSaveStatus() { if (enWebPersistenceThing.currentState == Components.interfaces.nsIWebBrowserPersist.PERSIST_STATE_SAVING) { // still saving...do nothing //dump("Still saving...\n"); return; } //dump("Done saving. State is: " + enWebPersistenceThing.currentState + "\n"); //dump("Result is: " + enWebPersistenceThing.result + "\n"); // save is finished, remove our interval timer clearInterval(enSaveFinishedInterval); // send it to evernote try { var enClipper = Components.classes["@evernote.com/ENMacMozillaClipper;1"].createInstance(Components.interfaces.EvernoteMacMozillaClipper); enClipper.clipFile(enSaveMainFile.path, content.document.title, content.document.location.href, enDocumentToClip.inputEncoding); return; } catch (e) { dump("Exception trying to clip:" + e); } // failed to clip with native lib, fall back to javascript clipper evernote_javascriptClipperFallback(); }